package NewsController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.util.Encrypt;
import com.renjun.smweb.controller.NewsController.NewsController;
import com.renjun.smweb.dao.pojo.LoginInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lrs on 2017/2/28.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/spring-mvc.xml", "classpath*:/spring-mybatis.xml"})
public class NewsControllerTestCase {

    @Autowired
    private NewsController newsController;

    //模拟request,response
    private MockHttpServletRequest request;
    private static MockHttpServletResponse response;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        request.getSession().setAttribute("userinfo", getLoginfo());
//        response = new MockHttpServletResponse();
    }

    @Test
    public void testAddNews(){
        System.out.println("添加系统公告");
        //封装参数
        Map<String,Object> params = new HashMap<>();
        params.put("title", "放假3天");
        params.put("starttime", "2017-03-01");
        params.put("endtime", "2017-03-10");
        params.put("content", "2月29,30,31号放假3天");
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //添加
        JSONObject rs = newsController.addNews(request, response);
        //打印解压的数据
        String jiem = (String) rs.get("response");
        System.out.println("\n返回参数：\n"+Encrypt.jiem(jiem)+"\n");
    }

    @Test
    public void testModifyNews(){
        System.out.println("修改系统公告");
        //封装参数
        Map<String,Object> params = new HashMap<>();
        params.put("id", "890f5c48-79b7-4199-9e85-db3932e693b6");
        params.put("title", "放假3天");
        params.put("starttime", "2017-03-03");
        params.put("endtime", "2017-03-15");
        params.put("content", "2月29,30,31号放假3天");
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //添加
        JSONObject rs = newsController.modifyNews(request, response);
        //打印解压的数据
        String jiem = (String) rs.get("response");
        System.out.println("\n返回参数：\n"+Encrypt.jiem(jiem)+"\n");
    }

    @Test
    public void testQueryNews(){
        System.out.println("查询系统公告/系统公告列表");
        //封装参数
        Map<String,Object> params = new HashMap<>();
        params.put("title", "");
        params.put("starttime", "");
        params.put("endtime", "");
        params.put("content", "");
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //添加
        JSONObject rs = newsController.queryNews(request, response);
        //打印解压的数据
        String jiem = (String) rs.get("response");
        System.out.println("\n返回参数：\n"+Encrypt.jiem(jiem)+"\n");
    }

    @Test
    public void testGetNews(){
        System.out.println("查看系统公告/系统公告详情");
        //封装参数
        request.setContent("{\"id\":\"890f5c48-79b7-4199-9e85-db3932e693b6\"}".getBytes());
        //添加
        JSONObject rs = newsController.getNews(request, response);
        //打印解压的数据
        String jiem = (String) rs.get("response");
        System.out.println("\n返回参数：\n"+Encrypt.jiem(jiem)+"\n");
    }

    @Test
    public void testCancelNews(){
        System.out.println("删除系统公告");
        //封装参数
        request.setContent("{\"id\":\"890f5c48-79b7-4199-9e85-db3932e693b6\"}".getBytes());
        //添加
        JSONObject rs = newsController.cancelNews(request, response);
        //打印解压的数据
        String jiem = (String) rs.get("response");
        System.out.println("\n返回参数：\n"+Encrypt.jiem(jiem)+"\n");
    }

    @Test
    public void testRecoverNews(){
        System.out.println("恢复系统公告");
        //封装参数
        request.setContent("{\"id\":\"890f5c48-79b7-4199-9e85-db3932e693b6\"}".getBytes());
        //添加
        JSONObject rs = newsController.recoverNews(request, response);
        //打印解压的数据
        String jiem = (String) rs.get("response");
        System.out.println("\n返回参数：\n"+Encrypt.jiem(jiem)+"\n");
    }

    @Test
    public void testLoginQueryNewsList(){
        System.out.println("管理员登录后系统公告弹出框信息");
        //封装参数
        request.setContent("".getBytes());
        //添加
        JSONObject rs = newsController.loginQueryNewsList(request, response);
        //打印解压的数据
        String jiem = (String) rs.get("response");
        System.out.println("\n返回参数：\n"+Encrypt.jiem(jiem)+"\n");
    }






    //获取缓存LoginInfo
    private LoginInfo getLoginfo() {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId("测试123");
        loginInfo.setRoleid("2");
        loginInfo.setDistrictcode("310116");
        loginInfo.setRoletype("2");
        loginInfo.setUsername("测试123");
        loginInfo.setRealname("junit测试管理员");
        return loginInfo;
    }
}
